package tech.wowo.controller;

import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import tech.wowo.pojo.Candidate;
import tech.wowo.pojo.VoteActivity;
import tech.wowo.service.CandidateService;
import tech.wowo.service.VoteActivityService;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: ShanYuqing
 * @Version: v1.0
 */
@RestController
@RequestMapping("voteactivity")
@Log4j
public class VoteActivityController {
    @Autowired
    private VoteActivityService voteActivityService;

    @RequestMapping("/list")
    public Map<String,Object> getCandidateList(){
        log.info("----VoteActivityServiceImpl---getVoteActivityList method--");
        Map<String,Object> result = new HashMap<>();

        List<VoteActivity> voteActivityList = voteActivityService.getVoteActivityList();
        result.put("message","200");
        result.put("voteActivityList",voteActivityList);

        return result;


    }

    @RequestMapping("/add")
    public Map<String,Object> addvoteactivity(VoteActivity voteActivity, MultipartFile file) {
        log.info("----CandidateServiceImpl---getCandidate method--");
        Map<String, Object> result = new HashMap<>();


        //文件上传-开始
        //判断文件是否为空
        if (!file.isEmpty()){
            //获取文件名 =  UUID + 原始文件名
            String imgName = UUID.randomUUID()+file.getOriginalFilename();
            // 需要复制你自己的tomcat的部署路径 + \\voteImg\\voteImg\\
            String path = "D:\\Server\\apache-tomcat-8.5.93-windows-x64\\apache-tomcat-8.5.93\\webapps\\voteImg\\voteImg\\";
            //执行上传
            try {
                file.transferTo(new File(path+imgName));
            } catch (IOException e) {
                e.printStackTrace();
                result.put("message","202");
                return result;
            }
            voteActivity.setVoteImg("/voteImg/voteImg/"+imgName);
        }
        //文件上传-结束

        Integer i = voteActivityService.addvoteactivity(voteActivity);

        result.put("message", "200");
        result.put("i", i);

        return result;
    }
}
